package com.example.helloword.ui;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.databinding.library.baseAdapters.BR;
import androidx.recyclerview.widget.GridLayoutManager;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.example.helloword.R;
import com.example.helloword.bean.Book;
import com.example.helloword.databinding.ActivityIntrodutionToBookBinding;
import com.example.helloword.ui.recyclerview.BindingToRecyclerviewAdapter;
import com.example.helloword.ui.recyclerview.GridSpacingItemDecoration;

import java.io.Serializable;

public class IntrodutionToBookActivity extends AppCompatActivity {

    private Book book=null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityIntrodutionToBookBinding viewDataBinding = DataBindingUtil.setContentView(this, R.layout.activity_introdution_to_book);
        book = (Book)getIntent().getSerializableExtra("book");
        viewDataBinding.bookInLy.setVariable(BR.book,book);
        viewDataBinding.chapterRv.setLayoutManager(new GridLayoutManager(this,2));
        viewDataBinding.chapterRv.addItemDecoration(new GridSpacingItemDecoration(2,10,false));
        BindingToRecyclerviewAdapter bindingToRecyclerviewAdapter = new BindingToRecyclerviewAdapter(book.getChapters(), R.layout.rv_item_chapter, BR.chapter);

        bindingToRecyclerviewAdapter.setItemClickListener(new BindingToRecyclerviewAdapter.ItemClickListener() {
            @Override
            public void setClickEvent(ViewDataBinding binding, int position) {
                binding.getRoot().setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Log.v("setItemClickListener","book.toString()");

                        Intent intent = new Intent(IntrodutionToBookActivity.this, PagesInReadActivity.class);
                        intent.putExtra("book",book);
                        intent.putExtra("current_chatper",position);
                        startActivity(intent);
                    }
                });

            }
        });
        viewDataBinding.chapterRv.setAdapter(bindingToRecyclerviewAdapter);


        Log.v("getSerializableExtra",book.toString());


    }
}